package com.recharge.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.recharge.common.enums.ErrorCode;
import com.recharge.common.result.ResultDO;
import com.recharge.common.util.BeanUtilsExtends;
import com.recharge.core.entity.MerchantRepaymentEntity;
import com.recharge.core.mapper.MerchantRepaymentMapper;
import com.recharge.domain.vo.MerchantRepaymentDO;
import com.recharge.service.MerchantRepaymentService;
import org.springframework.stereotype.Service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Reagan
 * @since 2020-10-07
 */
@Service
public class MerchantRepaymentServiceImpl extends ServiceImpl<MerchantRepaymentMapper, MerchantRepaymentEntity> implements MerchantRepaymentService {

    private Logger logger = LoggerFactory.getLogger(this.getClass().getName());

    @Override
    public ResultDO add(MerchantRepaymentDO domain) {
        MerchantRepaymentEntity entity = new MerchantRepaymentEntity();
        ResultDO result = BeanUtilsExtends.copy(entity, domain);
        if (!result.isSuccess()) {
            logger.debug("error code：" + result.getErrorCode().getCode() + " msg" + result.getErrorCode().getMsg());
            return result;
        }
        boolean save = true;
        try {
            save = this.save(entity);
        } catch(Exception e) {
            result.setSuccess(false);
            result.setErrorCode(ErrorCode.SYSTEM_EXCEPTION);
            logger.debug("merchantRepaymentService-add error code：" + ErrorCode.SYSTEM_EXCEPTION.getCode() + " msg" + ErrorCode.SYSTEM_EXCEPTION.getMsg());
            return result;
        }
        //判断r的值是否小于0.如果小于0 就代表添加失败
        if (!save) {
            result.setSuccess(false);
            result.setErrorCode(ErrorCode.SYSTEM_EXCEPTION);
            logger.debug("添加失败");
            return result;
        }

        domain = getDomain(entity);

        result.setModel(ResultDO.FIRST_MODEL_KEY, domain);
        return result;
    }

    @Override
    public ResultDO get(String id) {
        // TODO
        return null;
    }

    @Override
    public ResultDO edit(MerchantRepaymentDO domain) {
        // TODO
        return null;
    }

    @Override
    public ResultDO find(MerchantRepaymentDO domain) {
        // TODO
        return null;
    }

    private MerchantRepaymentDO getDomain(MerchantRepaymentEntity entity) {

        //判断房源对象是否为空，为空就返回 空，不为空就进行下面的代码
        if(entity == null) return null;
        MerchantRepaymentDO domain = new MerchantRepaymentDO();

        domain = BeanUtilsExtends.copyProperties(domain, entity) ? domain : null;

        //判断拷贝数据到Dto对象是否成功，如果成功则返回domian，如果不成功就返回空
        return domain;
    }

}
